package com.hd.crdao.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.base.MyBaseAdapter;
import com.hd.crdao.bean.BankListBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/5/26 0026
 */
public class ChooseBankActivity extends BaseSecondActivity {
    @BindView(R.id.choosebank_head)
    ExpandHeadView mChoosebankHead;
    @BindView(R.id.gridview)
    GridView mGridview;
    final static int SEND_BANK_NAME = 101;
    public List<BankListBean.RowsBean> mBankListBeanRows;

    @Override
    protected int setContentView() {
        return R.layout.activity_choosebank;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mChoosebankHead;
    }

    @Override
    protected void initView() {
        initData();

        mGridview.setVerticalSpacing(20);
        mGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("bankNmae", mBankListBeanRows.get(position).getBcNote());
                intent.putExtra("bankCode", mBankListBeanRows.get(position).getBcCode());
                setResult(SEND_BANK_NAME, intent);
                finish();
            }
        });
    }


    private void initData() {

        OkHttpUtils.post().url(Constants.QUERY_BANKCODE_URL)
                .addParams("pageNo", "1")
                .addParams("pageSize", "")
                .addParams("sign", Constants.SIGN).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
              //  ALog.d(response);
                BankListBean bankListBean = new Gson().fromJson(response, BankListBean.class);
                mBankListBeanRows = bankListBean.getRows();
                mGridview.setAdapter(new MyGridAdapter(mBankListBeanRows));

            }
        });

        /*bankslist.add(0, "农业银行");
        bankslist.add(1, "交通银行");
        bankslist.add(2, "中国银行");
        bankslist.add(3, "建设银行");
        bankslist.add(4, "光大银行");
        bankslist.add(5, "兴业银行");
        bankslist.add(6, "招商银行");
        bankslist.add(7, "民生银行");
        bankslist.add(8, "工商银行");*/


    }


    private class MyGridAdapter extends MyBaseAdapter<BankListBean.RowsBean> {

        private HashMap<String, Integer> map;

        public MyGridAdapter(List<BankListBean.RowsBean> dataSets) {
            super(dataSets);
            map = new HashMap<>();
            map.put("ABC", R.drawable.choice_nongye_2x);  //农业银行
            map.put("BCM", R.drawable.choice_jiaotong_2x);//交通银行
            map.put("BOC", R.drawable.choice_zhongguo_2x);//中国银行
            map.put("CCB", R.drawable.choice_jianshe_2x);//建设银行
            map.put("CEB", R.drawable.choice_guangda_2x);//光大银行
            map.put("CIB", R.drawable.choice_xingye_2x);//兴业银行
            map.put("CMB", R.drawable.choice_zhaoshang_2x);//招商银行
            map.put("CMBC", R.drawable.choice_minsheng_2x);//民生银行
            map.put("ICBC", R.drawable.choice_gongshang_2x);//工商银行
            map.put("SPDB", R.drawable.choice_pufa_2x);//浦发银行
            map.put("PAB", R.drawable.choice_pingan_2x);    //平安银行
            map.put("ECITIC", R.drawable.choice_zhongxin_3x);//中信银行
            map.put("HXB", R.drawable.choice_huaxia_2x);    //华夏银行
            map.put("PSBC", R.drawable.choice_youzheng_2x);//邮政银行
            map.put("RCB", R.drawable.choice_xinyongshe_2x);//信用社
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(UIUtils.getContext(), R.layout.choosebank_item, null);
                holder.image = (ImageView) convertView.findViewById(R.id.choose_bankimage);
                holder.bankname = (TextView) convertView.findViewById(R.id.choose_bankname);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            BankListBean.RowsBean rowsBean = mDataSets.get(position);
            String bcCode = rowsBean.getBcCode();
            if (map.containsKey(bcCode)) {
                int resId = map.get(bcCode);
                holder.image.setImageResource(resId);
            } else {
                holder.image.setImageResource(R.drawable.choice_moren_2x);
            }
            holder.bankname.setText(rowsBean.getBcNote());

            return convertView;
        }

        class ViewHolder {
            ImageView image;
            TextView bankname;
        }


    }

}
